package 大作业1;
//火星推送
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
//媛媛推送
public class Demo3 {
	 public static void main(String[] args) throws Exception {
	    	int i=-1;
	    	BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
	    	while(i!=5) {
	    		System.out.println("======================");
	    		System.out.println("欢迎使用密码管理系统");
	    		System.out.println("======================");
	    		System.out.println("请选择操作:");
	    		System.out.println("1:加密");
	    		System.out.println("2:解密");
	    		System.out.println("3:判断密码强度");
	    		System.out.println("4:密码生成");
	    		System.out.println("5:退出");
	    		System.out.print("请输入选项序号:");
	    		try {
	    		i=Integer.parseInt(br.readLine());
	    		processOption(i);}catch(Exception e) {
	    			e.printStackTrace();
	    		}
	    		switch(i) {
	    		case 1:
	    			System.out.println("======================");
	        		System.out.println("欢迎使用密码管理系统");
	        		System.out.println("======================");
	        		System.out.print("请输入要加密的密码:");
	        		String pW=br.readLine();
	        		try {
	        		eInput(pW);
	        		System.out.println("加密后的结果是:"+encryption(pW,3));
	        		}catch(Exception e) {
	        			e.printStackTrace();
	        		}
	        		break;
	    		case 2:
	    			System.out.println("======================");
	        		System.out.println("欢迎使用密码管理系统");
	        		System.out.println("======================");
	        		System.out.print("请输入要解密的密码:");
	        		String npw=br.readLine();
	        		System.out.println("解密后的结果是:"+decrypt(npw,3));
	        		break;
	    		case 3:
	    			System.out.println("======================");
	        		System.out.println("欢迎使用密码管理系统");
	        		System.out.println("======================");
	        		System.out.print("请输入要判断强度的密码:");
	        		String pW3=br.readLine();
	        		try {
	        			eInput(pW3);
	        			System.out.println("该密码的强度为"+secure(pW3));	
	        		}catch(Exception e) {
	        			e.printStackTrace();
	        		}
	        		break;
	    		case 4:
	    			System.out.println("======================");
	        		System.out.println("欢迎使用密码管理系统");
	        		System.out.println("======================");
	        		System.out.print("请输入密码长度:");
	        		int length=Integer.parseInt(br.readLine());
	        		System.out.println("随机生成密码:"+passwordGenerato(length));
	        		break;
	    		case 5:
	    			i=5;
	    			System.out.println("退出成功");
	    			break;
	    		}
	    		}
	 }
	//选择选项
     public static void processOption(int option) throws Exception {
         if (option <= 0 || option > 5) {
             throw new Exception("没有该选项序号，请重新输入");
         }
     }
     //非法密码异常生成
     public static void eInput(String password) throws Exception {
  	   if(!isValidPassword(password)) {
  		   throw new Exception("输入密码格式不正确,请重新输入");
  	   }
     }
     //加密操作
     public static String encryption(String pw,int offset) {
  	 //将每个字符的ASCII码加上它在字符串中的位置(1开始)和偏移值3
  	   StringBuilder sb=new StringBuilder();
  	   for(int i=0;i<pw.length();i++) {
  		   char c=pw.charAt(i);
  		   char newchar=(char)(c+1+i+offset);
  		   sb.append(newchar);
  	   }
     if(sb.length()>=2) {
  	   char first=sb.charAt(0);
         char last=sb.charAt(sb.length()-1);
         sb.setCharAt(0,last);
         sb.setCharAt(sb.length()-1,first);
     }
     String newPw=sb.reverse().toString();
     return newPw;
     }
     //解密操作
     public static String decrypt(String npw,int offset) {
  	   StringBuilder sb=new StringBuilder();
  	   for(int i=0;i<npw.length();i++) {
  		   sb.append(npw.charAt(i));
  	   }
  	   StringBuilder opw_ing=sb.reverse();
  	   if(opw_ing.length()>=2) {
  		   char first=opw_ing.charAt(0);
  		   char last=opw_ing.charAt(opw_ing.length()-1);
  		   opw_ing.setCharAt(0, last);
  		   opw_ing.setCharAt(opw_ing.length()-1,first);
  	   }
  	   StringBuilder sb2=new StringBuilder();
  	   for(int i=0;i<opw_ing.length();i++) {
  		   char c=opw_ing.charAt(i);
  		   char oldchar=(char)(c-1-i-offset);
  		   sb2.append(oldchar);
  	   }
  	   String oldPw=sb2.toString();
  	   return oldPw;
     }
   //判断密码强度
     public static String secure(String password) {
  	   boolean oneNumber=false;
  	   boolean uppercase = false;
         boolean lowercase = false;
         boolean allNumber=false;
         boolean allLetter=false;
         int aN=0;
         int aL=0;
  	   char[] ca=password.toCharArray();
  	   for(char c:ca) {
  		   if(Character.isDigit(c)) {
  			   oneNumber=true;
  		   }
  		   if(Character.isUpperCase(c)) {
  			   uppercase=true;
  		   }
  		   if(Character.isLowerCase(c)) {
  			   lowercase=true;
  		   }
  	   }
  	   for(char c:ca) {
  		   if(Character.isDigit(c)) {
  			   aN++;
  		   }
  		   if(Character.isLetter(c)) {
  			   aL++;
  		   }
  	   }
  	   if(aN==password.length()) {
  		   allNumber=true;
  	   }
  	   if(aL==password.length()) {
  		   allLetter=true;
  	   }
  	   if((password.length()<8)||(password.length()>=8&&(allNumber||allLetter))){
  		   return "弱强度";
  	   }else if(password.length()>=8&&oneNumber&&!(uppercase&&lowercase)&&(uppercase||lowercase)){
  		   return "中强度";
  	   }else if(password.length()>=8&&oneNumber&&uppercase&&lowercase) {
  		   return "高强度";
  	   }else{
  		   return "密码不合法";
  	   }
     }
     //密码生成
     public static String passwordGenerato(int length) {
  	   if(length>=8) {
  		   String lLetter="qwertyuiopasdfghjklzxcvbnm";
      	   String uLetter="QWERTYUIOPASDFGHJKLZXCVBNM";
      	   String aNumber="1234567890";
      	   StringBuilder sb=new StringBuilder();
      	   String allChars=lLetter+uLetter+aNumber;
      	   Random r=new Random();
      	   sb.append(randomChar(aNumber));//一个数字
      	   sb.append(randomChar(uLetter));//一个大写字母
      	   sb.append(randomChar(lLetter));//一个小写字母
      	   // 生成剩余部分的密码
             for (int i = 0; i < length - 3; i++) {
                 sb.append(randomChar(allChars));
             }
             //打乱密码顺序
             String pw=sb.toString();
             char[] c=pw.toCharArray();
             for(int i=0;i<length;i++) {
          	   int randomIndex=r.nextInt(pw.length());
                 char temp=c[randomIndex];
                 c[randomIndex]=c[i];
                 c[i]=temp;
             }
             return new String(c); 
  	   }else {
  		   return "输入长度必须大于等于8";
  	   }
     }
     public static char randomChar(String characters) {
         Random random = new Random();
         int randomIndex = random.nextInt(characters.length());
         return characters.charAt(randomIndex);
     }
     //用户密码判断是否正确输入
     public static boolean isValidPassword(String password) {
         // 定义正则表达式
         String regex = "[a-zA-Z0-9]{1,16}";
         // 编译正则表达式
         Pattern pattern = Pattern.compile(regex);
         // 创建Matcher对象
         Matcher matcher = pattern.matcher(password);
         // 判断是否匹配
         return matcher.matches();
     }
}
